﻿#region references
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Web.Security;

/* Project dependencies */
using IcaGames.Logging;
using IcaGames.Database;
using IcaGames.Objects;
#endregion

namespace IcaGames.Database.ObjectManagement
{
    /*
     * Class that holds common methods used to manage User objects.
     * 
     * @author
     * Rick Oonk
     * 
     * @version
     * 1.0
     * 
     * @update
     * 20-11-2009 by Rick Oonk.
     * Created Class and added attributes and standard functions. 
     */
    public static class UserManagement
    {
        /*
         * Function to validate a user given a specified email and password.
         * 
         * @param
         * email: The email of the specified user.
         * password: The password of the specified user.
         * 
         * @returns
         * True if the user was succesfully validated.
         * False if the user was not succesfully validated.
         */
        public static bool ValidateUser(string email, string password)
        {
            SqlCommand command = null;
            SqlDataReader dr = null;
            string commandText = "spValidateUser";

            try
            {
                command = Connection.GetCommand(commandText);
                command.CommandType = CommandType.StoredProcedure;
                command.Parameters.AddWithValue("@email", email);
                command.Parameters.AddWithValue("@password", password);

                /* Get the SqlDataReader and check if it contains user rows */
                dr = Connection.GetSqlDataReader(command);
                if (!dr.HasRows) { return false; }
            }
            catch (Exception ex)
            {
                Log.CreateLog("Exception while validating user", "USERMANAGEMENT");
                Log.CreateLog(ex.Message, "USERMANAGEMENT");
                return false;
            }
            finally
            {
                Connection.CloseCommand(command);
            }

            return true;
        }

        /*
         * Function to get a user from the database using a specified e-mailaddress.
         * 
         * @param
         * email: The e-mailaddress of the user.
         * 
         * @returns
         * The MembershipUser object with all the user information.
         */
        public static int GetUserId(string email)
        {
            SqlCommand command = null;
            string commandText = "spGetUserId";
            int ret = 0;

            try
            {
                command = Connection.GetCommand(commandText);
                command.CommandType = CommandType.StoredProcedure;
                command.Parameters.AddWithValue("@email", email);

                ret = Convert.ToInt32(command.ExecuteScalar());
            }
            catch (Exception ex)
            {
                Log.CreateLog("Exception while retrieving userId", "USERMANAGEMENT");
                Log.CreateLog(ex.Message, "USERMANAGEMENT");
                ret = -1;
            }
            finally
            {
                Connection.CloseCommand(command);
            }

            return ret;
        }

        /*
         * Function that retrieves a user from the database using a specified userId.
         * 
         * @param
         * userId: The userId of the user that has to be retrieved.
         * user: The MembershipUser object that has to be loaded.
         * 
         * @returns
         * True if the user has succesfully been loaded.
         * False if the user has not succesfully been loaded.
         */ 
        public static bool GetUser(int userId, User user)
        {
            SqlCommand command = null;
            SqlDataReader dr = null;
            string commandText = "spGetUser";
            try
            {
                command = Connection.GetCommand(commandText);
                command.Parameters.AddWithValue("userId", userId);
                command.CommandType = CommandType.StoredProcedure;

                dr = Connection.GetSqlDataReader(command);
                while (dr.Read())
                {
                    user.UserId = Cast.CastToInteger(dr["KLANT_ID"]);
                    user.Email = Cast.CastToString(dr["EMAILADRES"]);
                    user.Password = Cast.CastToString(dr["WACHTWOORD"]);
                    user.FirstName = Cast.CastToString(dr["VOORNAAM"]);
                    user.LastName = Cast.CastToString(dr["ACHTERNAAM"]);
                    user.Prefix = Cast.CastToString(dr["AANHEF"]);
                }
            }
            catch (Exception ex)
            {
                Log.CreateLog("Exception while retrieving user", "USERMANAGEMENT");
                Log.CreateLog(ex.Message, "USERMANAGEMENT");
                return false;
            }
            finally
            {
                Connection.CloseCommand(command, dr);
            }
            return true;
        }

        /*
         * Function to create a new user and store the user information in the database.
         * 
         * @param
         * user: The User object containing the user information.
         * 
         * @returns
         * True if the user was succesfully inserted.
         * False if the user was not succesfully inserted.
         */ 
        public static bool AddUser(User user)
        {
            SqlCommand command = null;
            string commandText = "spAddUser";

            try
            {
                command = Connection.GetCommand(commandText);
                command.Parameters.AddWithValue("@email", user.Email);
                command.Parameters.AddWithValue("@password", user.Password);
                command.Parameters.AddWithValue("@firstName", user.FirstName);
                command.Parameters.AddWithValue("@lastName", user.LastName);
                command.Parameters.AddWithValue("@date", DateTime.Now);
                command.Parameters.AddWithValue("@prefix", user.Prefix);
                command.CommandType = CommandType.StoredProcedure;

                command.ExecuteNonQuery();
            }
            catch (Exception ex)
            {
                Log.CreateLog("Exception while inserting new user", "USERMANAGEMENT");
                Log.CreateLog(ex.Message, "USERMANAGEMENT");
                return false;
            }
            finally
            {
                Connection.CloseCommand(command);
            }

            return true;
        }
    }
}
